package com.abe.mvp.view.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.abe.config.SettingParam;
import com.abe.mvp.model.bean.MenuBean;
import com.abe.mvp.model.bean.SettingBean;
import com.abe.mvp.view.activity.FragmentCallbackListener.SettingCallbackListener;
import com.abe.mvp.view.adapter.SettingMenuAdapter;
import com.abe.mvp.view.fragment.base.BaseSettingFragment;
import com.abe.threadmill.R;

public class SettingWifiFragment extends BaseSettingFragment {

	@SuppressWarnings("unused")
	private SettingCallbackListener callback;

	private ListView setwifi_list;
	private List<MenuBean> menuBeans;
	private SettingMenuAdapter adapter;

	private LinearLayout set_wifi_soket;
	private Button set_wifi_socket_button_save;
	private Button set_wifi_socket_button_save_cancel;
	private EditText inputIP1;
	private EditText inputIP2;
	private EditText inputIP3;
	private EditText inputIP4;
	private EditText inputPORT;

	private String IP;
	private int Port;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		callback = (SettingCallbackListener) getActivity();
		View view = this.onCreateView(inflater,
				R.layout.activity_set_fragment_wifi, container,
				savedInstanceState);
		initView(view);
		return view;
	}

	public void initView(View view) {
		set_wifi_soket = (LinearLayout) view.findViewById(R.id.set_wifi_soket);
		setwifi_list = (ListView) view.findViewById(R.id.setwifi_list);
		set_wifi_soket.setVisibility(View.GONE);
		setwifi_list.setVisibility(View.VISIBLE);
		/** wifi socket */
		menuBeans = new ArrayList<MenuBean>();
		for (int i = 0; i < SettingParam.wifis.length; i++) {
			MenuBean bean = new MenuBean();
			bean.setNameStr(SettingParam.wifis[i]);
			bean.setSelected(false);
			menuBeans.add(bean);
		}
		adapter = new SettingMenuAdapter(getActivity(), menuBeans, scale,
				SettingMenuAdapter.TYPE_LIST);
		setwifi_list.setAdapter(adapter);
		setwifi_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int pos,
					long id) {
				if (SettingParam.wifis[pos].equals(SettingParam.wifi)) {
					/** 系统WIFI设置页面 */
					if (android.os.Build.VERSION.SDK_INT > 10) {
						// 3.0以上打开设置界面，也可以直接用ACTION_WIRELESS_SETTINGS打开到wifi界面
						startActivity(new Intent(
								android.provider.Settings.ACTION_SETTINGS));
					} else {
						startActivity(new Intent(
								android.provider.Settings.ACTION_WIRELESS_SETTINGS));
					}
				} else {
					/** 隐藏列表，显示socket设置 */
					set_wifi_soket.setVisibility(View.VISIBLE);
					setwifi_list.setVisibility(View.GONE);
				}
			}
		});

		/*********** IP与Port ***********/
		set_wifi_socket_button_save_cancel = (Button) view
				.findViewById(R.id.set_wifi_socket_button_save_cancel);
		set_wifi_socket_button_save = (Button) view
				.findViewById(R.id.set_wifi_socket_button_save);
		inputIP1 = (EditText) view.findViewById(R.id.inputIP1);
		inputIP2 = (EditText) view.findViewById(R.id.inputIP2);
		inputIP3 = (EditText) view.findViewById(R.id.inputIP3);
		inputIP4 = (EditText) view.findViewById(R.id.inputIP4);
		inputPORT = (EditText) view.findViewById(R.id.inputPORT);

		SettingBean settingBean = SettingParam.getSettingBean();
		String[] ips = settingBean.getSocketIp().split("\\.");
		System.out.println("settingBean="+ips.length);
		inputIP1.setText(ips[0]);
		inputIP2.setText(ips[1]);
		inputIP3.setText(ips[2]);
		inputIP4.setText(ips[3]);
		inputPORT.setText(String.valueOf(settingBean.getSocketPort()));

		OnClickListener clickListener = new OnClickListener() {

			@Override
			public void onClick(View view) {
				switch (view.getId()) {
				case R.id.set_wifi_socket_button_save:
					String ip1 = getEditString(inputIP1);
					String ip2 = getEditString(inputIP2);
					String ip3 = getEditString(inputIP3);
					String ip4 = getEditString(inputIP4);
					String port = getEditString(inputPORT);
					if (!ip1.equals("") && !ip2.equals("") && !ip3.equals("")
							&& !ip4.equals("") && !port.equals("")) {
						StringBuilder builder = new StringBuilder();
						IP = builder.append(ip1).append(".").append(ip2)
								.append(".").append(ip3).append(".")
								.append(ip4).toString();
						Port = Integer.parseInt(port);
						saveChange();
						set_wifi_soket.setVisibility(View.GONE);
						setwifi_list.setVisibility(View.VISIBLE);
					} else {
						showToast(R.string.common_ipport_empty);
					}
					break;
				case R.id.set_wifi_socket_button_save_cancel:
					set_wifi_soket.setVisibility(View.GONE);
					setwifi_list.setVisibility(View.VISIBLE);
					break;

				}

			}
		};
		set_wifi_socket_button_save_cancel.setOnClickListener(clickListener);
		set_wifi_socket_button_save.setOnClickListener(clickListener);
	}

	@Override
	protected void saveChange() {
		SettingBean settingBean = SettingParam.getSettingBean();
		settingBean.setSocketIp(IP);
		settingBean.setSocketPort(Port);
		SettingParam.updateSettingBean(settingBean);
	}

	private String getEditString(EditText editText) {
		return editText.getText().toString().trim();
	}
}
